package com.xpqh.ai.config;

import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

/**
 * OpenAI配置类
 */
@Configuration
@ConfigurationProperties(prefix = "openai")
@Data
public class OpenAiConfig {
    
    /**
     * OpenAI API密钥
     */
    private String apiKey;
    
    /**
     * API基础URL，默认为OpenAI官方地址
     */
    private String baseUrl = "https://api.openai.com/v1";
    
    /**
     * 请求超时时间（秒）
     */
    private int timeout = 60;
    
    /**
     * 默认模型
     */
    private String defaultModel = "gpt-3.5-turbo";
    
    /**
     * 创建OpenAI客户端Bean
     */
    @Bean
    public OpenAIClient openAIClient() {
        if (!StringUtils.hasText(apiKey)) {
            throw new IllegalArgumentException("OpenAI API密钥不能为空，请在配置文件中设置 openai.api-key");
        }
        
        return OpenAIOkHttpClient.builder()
                .apiKey(apiKey)
                .baseUrl(baseUrl)
                .build();
    }
} 